package com.idanchuang.support.etcache.pubsub.redis;

import com.idanchuang.support.etcache.ConditionalOnEtCacheEnabled;
import com.idanchuang.support.etcache.pubsub.RemoteEventPublisher;
import com.idanchuang.support.etcache.pubsub.RemoteEventSubscriber;
import com.idanchuang.support.etcache.pubsub.SubEventQueue;
import com.idanchuang.support.etcache.remote.codec.RemoteKeyCodec;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author yjy
 * Created at 2022/3/28 5:08 下午
 */
@Configuration
@ConditionalOnEtCacheEnabled
@ConditionalOnProperty(prefix = "idanchuang.component.etcache", name = "eventType",
        havingValue = "redis", matchIfMissing = true)
public class RedisRemoteEventAutoConfiguration {

    @Value("${spring.application.name}")
    private String appId;

    @Bean
    @ConditionalOnMissingBean(RemoteEventSubscriber.class)
    public RemoteEventSubscriber redisRemoteEventSubscriber(RemoteKeyCodec remoteKeyCodec,
                                                            SubEventQueue subEventQueue,
                                                            RedisConnectionFactory connectionFactory) {
        return new RedisRemoteEventSubscriber(remoteKeyCodec, subEventQueue, appId, connectionFactory);
    }

    @Bean
    @ConditionalOnMissingBean(RemoteEventPublisher.class)
    public RemoteEventPublisher remoteEventPublisherFactory(RedisConnectionFactory connectionFactory) {
        return new RedisRemoteEventPublisher(appId, connectionFactory);
    }

}
